package com.boc.langxiang.ui.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.boc.langxiang.App;
import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.MyCollectionsActivityContract;
import com.boc.langxiang.entity.response.CollectEntity;
import com.boc.langxiang.presenter.me.MyCollectionsActivityPresenter;
import com.boc.langxiang.ui.activity.shop.ProductDetailActivity;
import com.boc.langxiang.ui.adapter.me.MyCollectionsRVAdapter;
import com.boc.langxiang.ui.adapter.me.SpacesItemDecoration;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyCollectionsActivity extends MeBaseToolBarActivity implements SwipeRefreshLayout.OnRefreshListener
        , MyCollectionsActivityContract.View, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.recylerview_me_mycollections)
    RecyclerView recylerviewMeMycollections;
    @BindView(R.id.swipeLayout_me_mycollections)
    SwipeRefreshLayout swipeLayoutMeMycollections;

    private boolean mVisible = false;

    private MyCollectionsRVAdapter mAdapter;
    private MyCollectionsActivityContract.Presenter mPresenter;
    private boolean mIsOnRefresh;
    private boolean mIsLoadmore;
    private int mPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collections);
        ButterKnife.bind(this);
        addTextMenu(getmToolbar());
        setToolBarTitle("我的收藏");
        swipeLayoutMeMycollections.setOnRefreshListener(this);
        swipeLayoutMeMycollections.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        recylerviewMeMycollections.setHasFixedSize(true);
        recylerviewMeMycollections.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        initAdapter();

        mPresenter = new MyCollectionsActivityPresenter(this);

        mPage = 1;
        myCollection();
    }

    private void myCollection() {
        mPresenter.myCollection(mPage);
    }

    private void initAdapter() {

        mAdapter = new MyCollectionsRVAdapter(mContext, null, this);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) recylerviewMeMycollections.getParent(), false);
        mAdapter.setEmptyView(emptyView);
        mAdapter.openLoadAnimation();
        mAdapter.isFirstOnly(false);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        recylerviewMeMycollections.addItemDecoration(new SpacesItemDecoration(this, 25));
        recylerviewMeMycollections.setAdapter(mAdapter);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                CollectEntity item = mAdapter.getItem(position);
                Intent intent = new Intent(mContext, ProductDetailActivity.class);
                intent.putExtra("id", item.getProduct_id());
                startActivity(intent);
            }
        });

        recylerviewMeMycollections.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mIsOnRefresh) {
                    return true;
                }
                return false;
            }
        });
    }


    @Override
    protected int setToolBarBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected int setToolBarBackImg() {
        return R.drawable.icon_me_left;
    }

    @Override
    protected int setToolBarTitleColor() {
        return Color.BLACK;
    }

    private void addTextMenu(Toolbar toolbar) {
        View menu = getLayoutInflater().inflate(R.layout.menu_me_myexercise_edit, null);
        final TextView text = (TextView) menu.findViewById(R.id.tv_me_myexercise_edit);
        text.setTextColor(ContextCompat.getColor(App.getInstance(), R.color.colorSecondaryText));
        text.setText("编辑");
        Toolbar.LayoutParams menuParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        menuParams.gravity = Gravity.RIGHT;
        toolbar.addView(menu, menuParams);
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<CollectEntity> data = mAdapter.getData();
                if (data == null || data.isEmpty()) {
                    text.setText("编辑");
                    return;
                }
                if (text.getText().toString().equals("编辑")) {
                    text.setText("完成");
                } else {
                    text.setText("编辑");
                }
                mAdapter.setDeleteIconVisiblity(!mVisible);
                mVisible = !mVisible;
            }
        });
    }

    @Override
    public void onRefresh() {
        if (mIsOnRefresh) {
            return;
        }
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mIsOnRefresh = true;
        mPage = 1;
        myCollection();
    }

    @Override
    public void onLoadMoreRequested() {
        recylerviewMeMycollections.post(new Runnable() {
            @Override
            public void run() {
                if (mIsLoadmore) {
                    return;
                }
                mIsLoadmore = true;
                mPage++;
                myCollection();
            }
        });
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        mIsLoadmore = false;
        mAdapter.notifyDataChangedAfterLoadMore(false);
        mIsOnRefresh = false;
        swipeLayoutMeMycollections.setRefreshing(false);
        T.showShort(mContext, msg);
    }

    @Override
    protected void onPause() {
        mVisible = false;
        super.onPause();
    }

    @Override
    public void delete(int position) {
        CollectEntity collectEntity = mAdapter.getData().get(position);
        mPresenter.delMycollect(collectEntity.getId(), position);

    }

    @Override
    public void myCollectionSuccess(List<CollectEntity> collect) {
        if (mIsLoadmore) {
            mIsLoadmore = false;
            if (collect == null) {
                mAdapter.notifyDataChangedAfterLoadMore(false);
                loadDone();
                return;
            }
            if (collect.size() < Constant.PAGE_SIZE) {
                mAdapter.notifyDataChangedAfterLoadMore(collect, false);
                loadDone();
            } else {
                mAdapter.notifyDataChangedAfterLoadMore(collect, true);
            }

        } else {
            mIsOnRefresh = false;
            swipeLayoutMeMycollections.setRefreshing(false);
            if (collect == null) {
                return;
            }
            mAdapter.setNewData(collect);
        }
    }

    @Override
    public void showDelLoading() {
        showProgressDialog("删除收藏...");
    }

    @Override
    public void delSuccess(int position) {
        mAdapter.getData().remove(position);
        mAdapter.notifyDataSetChanged();
    }

    private void loadDone() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_load_done, (ViewGroup) recylerviewMeMycollections.getParent(), false);
        mAdapter.addFooterView(view);
    }

}
